package io.renren.modules.lanling.controller;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import io.renren.common.validator.ValidatorUtils;
import io.renren.modules.lanling.entity.SubjectEntity;
import io.renren.modules.lanling.service.SchoolService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.renren.modules.lanling.entity.SignUpSubjectEntity;
import io.renren.modules.lanling.service.SignUpSubjectService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;

import static io.renren.modules.sys.shiro.ShiroUtils.getUserId;


/**
 * 报名科目表
 *
 * @author LANLING
 * @email lanling@gmail.com
 * @date 2018-10-26 21:18:44
 */
@RestController
@RequestMapping("lanling/signupsubject")
public class SignUpSubjectController {
    @Autowired
    private SignUpSubjectService signUpSubjectService;
    @Autowired
    private SchoolService schoolService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("lanling:signupsubject:list")
    public R list(@RequestParam Map<String, Object> params) {
        Long userId = getUserId();
        params.put("createUserId", userId);
        PageUtils page = signUpSubjectService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("lanling:signupsubject:info")
    public R info(@PathVariable("id") Integer id) {
        SignUpSubjectEntity signUpSubject = signUpSubjectService.selectById(id);

        return R.ok().put("signUpSubject", signUpSubject);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("lanling:signupsubject:save")
    public R save(@RequestBody SignUpSubjectEntity signUpSubject) {
        Long userId = getUserId();
        signUpSubject.setCreateUserId(userId);
        //查找lanling_school中id
        int schoolId = schoolService.querySchoolId(userId);
        signUpSubject.setSchoolId(schoolId);
        signUpSubject.setCreateDate(new Date());
        signUpSubjectService.insert(signUpSubject);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("lanling:signupsubject:update")
    public R update(@RequestBody SignUpSubjectEntity signUpSubject) {
        ValidatorUtils.validateEntity(signUpSubject);
        signUpSubject.setLastModifyDate(new Date());
        signUpSubjectService.updateAllColumnById(signUpSubject);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("lanling:signupsubject:delete")
    public R delete(@RequestBody Integer[] ids) {
        signUpSubjectService.deleteBatchIds(Arrays.asList(ids));
        return R.ok();
    }

    @RequestMapping("/queryList")
    public R queryList() {
        Long userId = getUserId();

        EntityWrapper<SignUpSubjectEntity> entityWrapper = new EntityWrapper<>();
        entityWrapper.eq("create_user_id", userId);

        List<SignUpSubjectEntity> signUpSubjectEntities = signUpSubjectService.selectList(entityWrapper);
        return R.ok().put("list", signUpSubjectEntities);
    }

}
